﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace IpToBlack.PUB
{
    public class gFunc
    {
        public static string SysPath = AppDomain.CurrentDomain.BaseDirectory;

        public static string null2str(string strTmp)
        {
            if (strTmp == null)
            {
                return "";
            }
            if (string.IsNullOrEmpty(strTmp) == true)
            {
                return "";
            }
            return strTmp;
        }
        public static string strFmtIn(string param)
        {
            string[] lsStr = param.Split(',');
            string lsStr2 = "";
            foreach (string strNo in lsStr)
            {
                lsStr2 = lsStr2 + "'" + strNo + "',";
            }
            lsStr2 = Left(lsStr2, lsStr2.Length - 1);

            return lsStr2;
        }
        public static string Left(string param, int length)
        {
            if (param.Length <= length)
            {
                return param;
            }
            string result = param.Substring(0, length);
            return result;
        }
        public static string Right(string param, int length)
        {
            if (param.Length <= length)
            {
                return param;
            }
            string result = param.Substring(param.Length - length, length);
            return result;
        }
        public static string Mid(string param, int startIndex, int length)
        {
            if (param.Length <= startIndex + length)
            {
                return param;
            }
            string result = param.Substring(startIndex, length);
            return result;
        }
        public static string Mid(string param, int startIndex)
        {
            string result = param.Substring(startIndex);
            return result;
        }
        public static string str2str(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return "";
            }
            s = s.Replace("=", "");
            s = s.Replace("'", "");
            s = s.Replace(";", "");
            s = s.Replace("\"", "");
            s = s.Replace("--", "");
            s = s.Replace("\r", "");
            s = s.Replace("\t", "");
            s = s.Replace("\n", "");
            s = s.Replace(" ", "");

            return s;
        }

        /// <summary>
        /// 写日志文本
        /// </summary>
        /// <param name="text"></param>
        public static void WriteLog(string text)
        {
            try
            {
                // 0: current method, i.e. GetCallerName
                // 1: 1 frame up current method: the method calling current method
                // 2: 2 frames up current method
                var source = "";
                //var callerMethod = new StackFrame(2, false)?.GetMethod();
                //if (callerMethod == null)
                //{
                //    source = "";
                //}
                //else
                //{
                //    source = $"{callerMethod.DeclaringType.FullName }.{callerMethod.Name}";
                //}

                gLog.Default.Info(text, source);
            }
            catch
            {

            }            
        }

        /// <summary>
        /// 取系统参数
        /// </summary>
        /// <param name="ls_varcode"></param>
        /// <returns></returns>
        public static string sysGetVar(string ls_varcode)
        {
            DB db = new DB();
            String lsSQL, ls_var_value;

            ls_var_value = "";
            lsSQL = @"SELECT sys_var_value FROM sys_t_system WHERE sys_var_id = '" + ls_varcode + "'";
            var lsRTN = db.QueryForFist<string>(lsSQL);

            return ls_var_value = null2str(lsRTN);
        }
        /// <summary>
        /// 保存系统参数
        /// </summary>
        /// <param name="ls_varcode"></param>
        /// <param name="ls_varvalue"></param>
        /// <returns></returns>
        public static string sysPutVar(string ls_varcode, string ls_varvalue)
        {
            DB db = new DB();
            String lsSQL;

            lsSQL = @"SELECT count(sys_var_value) FROM sys_t_system WHERE sys_var_id = '" + ls_varcode + "'";
            var count = db.QueryForFist<int>(lsSQL);

            if (count > 0)
            {
                lsSQL = @"UPDATE sys_t_system  SET sys_var_value = '" + ls_varvalue + "' WHERE sys_var_id = '" + ls_varcode + "'";
            }
            else
            {
                //如果没有发现该变量，系统只动增加
                lsSQL = @"INSERT into sys_t_system(sys_var_id, sys_var_name, sys_var_value,sys_var_desc) VALUES('" + ls_varcode + "', '系统变量" + ls_varcode + "', '" + ls_varvalue + "','')";
            }

            try
            {

                db.Execute(lsSQL);

                return "1";
            }
            catch (Exception er)
            {
                WriteLog(er.Message+er.StackTrace);
                return er.Message;
            }

        }

        public static string GetTextToIP(string text)
        {
            string input = "The IP address is 192.168.1.1 and the subnet mask is 255.255.255.0.";
            string pattern = @"\b(?:\d{1,3}\.){3}\d{1,3}\b";
            string ip = "";
            MatchCollection matches = Regex.Matches(text, pattern);

            foreach (Match match in matches)
            {
                ip = ip + match.Value;
                Console.WriteLine(match.Value);
            }
            return ip;
        }

    }
}
